package com.xiaok.netty.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.xiaok.common.enums.StatusCode;
import com.xiaok.common.response.BaseResponse;
import com.xiaok.netty.server.service.MonitorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Classname MonitorController
 * @Description 监控类控制器
 * @Date 2020/9/29 19:43
 * @Created by xiaok
 */
@Controller
@RequestMapping("/monitor")
public class MonitorController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    MonitorService monitorService;

    /**
     * 获取监控的数据
     * @return
     */
    @RequestMapping("/getData")
    @ResponseBody
    public BaseResponse<String> getData(){

        //校验成功,添加数据
        BaseResponse<String> response = new BaseResponse<String>(StatusCode.Success);
        try {
            Map<String,Object> map = monitorService.getData();
            response.setData(JSONObject.toJSONString(map));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("发送异常  :" +e.getMessage());
            response.setCode(StatusCode.Fail.getCode());
            response.setMsg("获取监控数据失败");
            return response;
        }
        return response ;


    }




}
